package yiran.dcolormotd;


import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.plugin.java.JavaPlugin;
import yiran.daobase.lang.LangHelper;
import yiran.daobase.output.MessageUtils;
import yiran.dcolormotd.manager.ConfigManager;
import yiran.dcolormotd.manager.IconManager;
import yiran.dcolormotd.manager.MotdManager;
import yiran.dcolormotd.tool.ServerPingListener;

import java.util.Objects;

public final class DColorMotd extends JavaPlugin {

    private ConfigManager configManager;
    private IconManager iconManager;
    private long serverStartTime;

    @Override
    public void onEnable() {

        serverStartTime = System.currentTimeMillis();

        String version = getServer().getBukkitVersion().split("-")[0];
        if (!DColorMotd.isVersionAtLeast(1, 13)) {
            MessageUtils.log("warn", LangHelper.getString("version_warning", version));
        } else {
            MessageUtils.log("info", "服务器版本: " + version);
        }

        // 使用 DaoBase 的初始化方法
        MessageUtils.init(this, "§a§l[刀牌MOTD] "); // 自定义前缀
        LangHelper.init(this);

        // 初始化管理器
        configManager = new ConfigManager(this);
        iconManager = new IconManager(this, configManager);
        MotdManager motdManager = new MotdManager(this, configManager);

        // 注册事件
        getServer().getPluginManager().registerEvents(new ServerPingListener(this, configManager, iconManager, motdManager), this);

        // 注册命令
        CommandExecutor reloadCommand = (sender, command, label, args) -> {
            if (args.length > 0 && args[0].equalsIgnoreCase("reload")) {
                if (sender.hasPermission("dcolormotd.reload")) {
                    reloadPlugin();
                    sender.sendMessage(MessageUtils.playerMsg("success", LangHelper.getString("reload_success")));
                    return true;
                }
            }
            sender.sendMessage(MessageUtils.playerMsg("error", LangHelper.getString("command_usage", "/dcolormotd reload")));
            return false;
        };

        Objects.requireNonNull(getCommand("dcolormotd")).setExecutor(reloadCommand);

        // 使用 DaoBase 的 banner 生成
        String[] banner = MessageUtils.getBanner(
            getDescription().getName(),
            String.join(", ", getDescription().getAuthors()),
            getDescription().getVersion()
        );

        for (String line : banner) {
            getLogger().info(line);
        }

        MessageUtils.log("info", LangHelper.getString("§a[DColorMotd]插件已启动"));
    }

    @Override
    public void onDisable() {
        MessageUtils.log("error", LangHelper.getString("§c[DColorMotd]插件已关闭"));
    }

    public void reloadPlugin() {
        configManager.reloadConfig();
        iconManager.reloadIcons();
        LangHelper.reload(); // 重载语言文件
    }

    public long getServerStartTime() {
        return serverStartTime;
    }

    public static boolean isVersionAtLeast(int major, int minor) {
        String[] parts = Bukkit.getBukkitVersion().split("-")[0].split("\\.");
        int serverMajor = Integer.parseInt(parts[0]);
        int serverMinor = Integer.parseInt(parts[1]);

        return serverMajor > major || (serverMajor == major && serverMinor >= minor);
    }

    // 添加兼容方法获取在线玩家数量
    public static int getOnlinePlayersCount() {
        if (isVersionAtLeast(1, 8)) {
            return Bukkit.getOnlinePlayers().size();
        } else {
            return Bukkit.getOnlinePlayers().toArray().length;
        }
    }
}



